package com.xyc.javaee.madp;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.xyc.javaee.madp.util.CollectParamConstant;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @Version 1.0
 * @Author:徐衍程
 * @Date:2022/4/18
 * @Content:
 */
public class BiTest {

    private static final String ENCODE = "app_id={appId}&app_secret={appSecret}&timestamp={timestamp}&data={data}";


	public static void main(String[] args) throws UnsupportedEncodingException {
        //上送数据签名校验
        biSignature();

    }

    /**
     * 上送数据签名校验
     * @throws UnsupportedEncodingException
     */
    private static void biSignature() throws UnsupportedEncodingException {
        String signature = "a2db116530ef2fa1189bded96fa53e9b";
        String appid = "4848438a8da44297";
        String appSecret = "fb727b459fd04029afa74c03f05675b8";
        String timestamp = "1650610468044";
        String data = "eyJpZGVudGl0aWVzIjp7IiRpZGVudGl0eV9sb2dpbl9pZCI6ImFkaHM4OTM0M25qZHM5ODczOTkzODAiLCIkaWRlbnRpdHlfY29va2llX2lkIjoiMTdmZTQ0OWRiNDEyNjUtMDgyNzc4OTcyNDRmODEtOTc3MWEzZi0yMDczNjAwLTE3ZmU0NDlkYjQyODcyIn0sImRpc3RpbmN0X2lkIjoiYWRoczg5MzQzbmpkczk4NzM5OTM4MCIsImxpYiI6eyIkbGliIjoianMiLCIkbGliX21ldGhvZCI6ImNvZGUiLCIkbGliX3ZlcnNpb24iOiIxLjIxLjUifSwicHJvcGVydGllcyI6eyIkdGltZXpvbmVfb2Zmc2V0IjotNDgwLCIkc2NyZWVuX2hlaWdodCI6MTA4MCwiJHNjcmVlbl93aWR0aCI6MTkyMCwiJHZpZXdwb3J0X2hlaWdodCI6ODcxLCIkdmlld3BvcnRfd2lkdGgiOjk5OCwiJGxpYiI6ImpzIiwiJGxpYl92ZXJzaW9uIjoiMS4yMS41IiwiJGxhdGVzdF90cmFmZmljX3NvdXJjZV90eXBlIjoidXJs55qEZG9tYWlu6Kej5p6Q5aSx6LSlIiwiJGxhdGVzdF9zZWFyY2hfa2V5d29yZCI6InVybOeahGRvbWFpbuino%2BaekOWksei0pSIsIiRsYXRlc3RfcmVmZXJyZXIiOiJ1cmznmoRkb21haW7op6PmnpDlpLHotKUiLCIkdXJsIjoiaHR0cDovL2xvY2FsaG9zdDo4MDgwLyMvaG9tZSIsIiRyZWZfc2NyZWVuX25hbWUiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvIy8iLCIkdXJsX3BhdGgiOiIvIiwiJHRpdGxlIjoicmFpbmJvdy1oNSIsImV2ZW50X2R1cmF0aW9uIjozNjExNDYuNSwiJGlzX2ZpcnN0X2RheSI6ZmFsc2UsIiRyZWZfc2NyZWVuX25hbWVfaG9zdCI6ImxvY2FsaG9zdCJ9LCJsb2dpbl9pZCI6ImFkaHM4OTM0M25qZHM5ODczOTkzODAiLCJhbm9ueW1vdXNfaWQiOiJ5eWRzSWQxIiwidHlwZSI6InRyYWNrIiwiZXZlbnQiOiIkV2ViUGFnZUxlYXZlIiwidGltZSI6MTY1MDYxMDQ2ODA0MywiX3RyYWNrX2lkIjo1NTIzMTgwNDQsIl9mbHVzaF90aW1lIjoxNjUwNjEwNDY4MDQ0fQ%3D%3D";
        Map<String, String> params = new HashMap<>(4);
        params.put(CollectParamConstant.APP_ID, appid);
        params.put(CollectParamConstant.APP_SECRET, appSecret);
        params.put(CollectParamConstant.TIMESTAMP, timestamp);
        params.put(CollectParamConstant.DATA, URLEncoder.encode(data, CharsetUtil.UTF_8));
        boolean flag = isIllegal(signature,params);
        System.out.println(flag);
    }

    /**
     * 上送数据是否非法
     * @param signature		采集客户端上送的摘要数据
     * @param params		采集客户端上述的数据
     * @return	非法：true,合法：false
     */
    public static boolean isIllegal(String signature, Map params) {
        String format = StrUtil.format(ENCODE, params);
        String md5Hex = DigestUtil.md5Hex(format);
        return StrUtil.equals(md5Hex, signature);
    }
}
